//
//  JYJVCOfLoveListen.m
//  HappyListen
//
//  Created by 冀永金 on 16/8/27.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJVCOfLoveListen.h"
#import "BaseCollectionView.h"
#import "JYJTAMacro.h"
#import "JYJTitleCollectionViewCell.h"
#import "BaseLabel.h"
#import "JYJLLBigCollectionVIewCell.h"
#import "JYJMusicMiniView.h"
#import "JYJSubscriptionCollectionViewCell.h"
#import "JYJVCOfFeaturedTopThreeDetails.h"
#import "JYJMusicPlayerViewController.h"
#import "JYJDownLoadCollectionViewCell.h"
#import "JYJNetWorkParser.h"
#import "Masonry.h"
#import "JYJDataTools.h"
@interface JYJVCOfLoveListen ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) BaseCollectionView *smallCollection;
@property (nonatomic, strong) UIView *redView;
@property (nonatomic, strong) BaseCollectionView *bigCollection;
@property (nonatomic, assign) NSInteger labelIndex;
@property (nonatomic, strong) NSIndexPath *oldIndexPath;
@property (nonatomic, strong) NSDictionary *infoDic;
@property (nonatomic, strong) NSMutableArray *marr;
@property (nonatomic, strong) NSString *name;
@end

@implementation JYJVCOfLoveListen

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"听你所爱";
    self.oldIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    [self congfigSubViews];
    [self creatBigCollection];
    [self createRedView];
    [self createKVOAction];
    self.view.backgroundColor = [UIColor colorWithRed:0.07 green:0.07 blue:0.08 alpha:1.00];
    
    // Do any additional setup after loading the view.
}

-(void)congfigSubViews {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 3, 50);
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollPositionCenteredHorizontally;
    self.smallCollection = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) collectionViewLayout:layout];
    [self.view addSubview:_smallCollection];
    self.smallCollection.delegate = self;
    self.smallCollection.dataSource = self;
    [self.smallCollection registerClass:[JYJTitleCollectionViewCell class] forCellWithReuseIdentifier:@"Title"];
    
}
- (void)creatBigCollection {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49 - 53);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    self.bigCollection = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 53, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49 - 53) collectionViewLayout:layout];
    [self.view addSubview:_bigCollection];
    self.bigCollection.delegate = self;
    self.bigCollection.dataSource = self;
    [self.bigCollection registerClass:[JYJLLBigCollectionVIewCell class] forCellWithReuseIdentifier:@"pool"];
    self.bigCollection.pagingEnabled = YES;
    self.bigCollection.scrollEnabled = NO;
    
    [self.bigCollection registerClass:[JYJSubscriptionCollectionViewCell class] forCellWithReuseIdentifier:@"subs"];
    [self.bigCollection registerClass:[JYJDownLoadCollectionViewCell class] forCellWithReuseIdentifier:@"downLoad"];
}
- (void)createRedView {
    self.redView = [[UIView alloc]initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH / 3, 3)];
    [self.view addSubview:_redView];
    _redView.backgroundColor = [UIColor redColor];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _smallCollection) {
        JYJTitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Title" forIndexPath:indexPath];
        NSArray *array = @[@"订阅",@"收藏",@"下载"];
        cell.titleLabel.text = array[indexPath.item];
        if (indexPath.row == _labelIndex) {
            cell.titleLabel.textColor = [UIColor whiteColor];
        }else {
            cell.titleLabel.textColor = [UIColor lightGrayColor];
        }
        if (indexPath.row == _oldIndexPath.item) {
            cell.titleLabel.textColor = [UIColor whiteColor];
        }else {
            cell.titleLabel.textColor = [UIColor lightGrayColor];
        }

        return cell;
        
    }else {
        if (indexPath.item == 0) {
            JYJSubscriptionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"subs" forIndexPath:indexPath];
            __weak typeof(self) weakSelf = self;
            cell.handelAction = ^(JYJVCOfFeaturedTopThreeDetails *details) {
                [weakSelf.navigationController pushViewController:details animated:YES];
               
            };
            cell.dic = self.infoDic;
            return cell;
        }else if (indexPath.item == 1){
            JYJLLBigCollectionVIewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
              __weak typeof(self) weakSelf = self;
            cell.handleMusic = ^(JYJMusicPlayerViewController *music) {
                weakSelf.tabBarController.tabBar.hidden = YES;
                weakSelf.hidesBottomBarWhenPushed = YES;
               [weakSelf.navigationController pushViewController:music animated:YES];
            };
            cell.dic = self.infoDic;
            return cell;
            
        }else {
            JYJDownLoadCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"downLoad" forIndexPath:indexPath];
            __weak typeof(self) weakSelf = self;
            cell.handleAction = ^(JYJMusicPlayerViewController *music) {
                weakSelf.tabBarController.tabBar.hidden = YES;
                weakSelf.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:music animated:YES];
            };

            cell.marr = self.marr;
            return cell;
        }
    }
}
// 添加观察者
- (void)createKVOAction {
    [self.bigCollection addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    CGFloat X = [[change objectForKey:@"new"]CGPointValue].x;
    CGFloat redX = X / 3;
    self.redView.transform = CGAffineTransformMakeTranslation(redX, 0);
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.bigCollection) {
        //当前大Collection的第几个item
        NSInteger currentItem = scrollView.contentOffset.x / SCREEN_WIDTH;
        // 根据第几个item获取当前的indexPath
        NSIndexPath *currentIndexPath = [NSIndexPath indexPathForItem:currentItem inSection:0];
        // 根据indexpaht获取当前的标题的Collection
        JYJTitleCollectionViewCell *cell = (JYJTitleCollectionViewCell *)[self.smallCollection cellForItemAtIndexPath:currentIndexPath];
        //字体变色
        cell.titleLabel.textColor = [UIColor whiteColor];
        // 恢复字体颜色
        NSIndexPath *indexPathPast = [NSIndexPath indexPathForItem:_labelIndex inSection:0];
        //加判防止滑一点点就就开，并没有划入新页面，字体就变色了
        if (currentIndexPath != indexPathPast) {
            JYJTitleCollectionViewCell *cell = (JYJTitleCollectionViewCell *)[self.smallCollection cellForItemAtIndexPath:indexPathPast];
            cell.titleLabel.textColor = [UIColor lightGrayColor];
        }
        //记录当前的item 和 划过的indexPath
        self.labelIndex = currentItem;
        self.oldIndexPath = currentIndexPath;
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.smallCollection) {
        [self.bigCollection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
        JYJTitleCollectionViewCell *cell = (JYJTitleCollectionViewCell *)[self.smallCollection cellForItemAtIndexPath:indexPath];
        cell.titleLabel.textColor = [UIColor whiteColor];
        JYJTitleCollectionViewCell *pastCell = (JYJTitleCollectionViewCell *)[self.smallCollection cellForItemAtIndexPath:_oldIndexPath];
        if (indexPath != _oldIndexPath) {
            pastCell.titleLabel.textColor = [UIColor lightGrayColor];
        }
        self.oldIndexPath = indexPath;
        self.labelIndex = _oldIndexPath.item;
    }
}
- (void)dealloc {
    [self.bigCollection removeObserver:self forKeyPath:@"contentOffset"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [JYJMusicMiniView shareMiniView].hidden = NO;
    NSUserDefaults *usersDefaults = [NSUserDefaults standardUserDefaults];
    self.infoDic = [usersDefaults objectForKey:@"info"];
    [self.bigCollection reloadData];
    self.tabBarController.tabBar.hidden = NO;
    self.hidesBottomBarWhenPushed = NO;
    NSString *path = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    NSString *musicPath = [path stringByAppendingPathComponent:@"musics.plist"];
    self.marr = [[NSKeyedUnarchiver unarchiveObjectWithFile:musicPath]mutableCopy];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
